import React, { useEffect, useState } from 'react';
import { PERMISSION_ENUM, ready } from '../bpm-sdk';

const ThirdForm = () => {
  const [sdk, setSdk] = useState<any>();

  useEffect(() => {
    async function init() {
      const instance: any = await ready({
        mode: 'iframe',
        tenant: 'CMI'
      });
      setSdk(instance);
      instance?.insertBtnForToolbar({
        position: 1,
        btns: [
          {
            name: '红色按钮',
            buttonType: 'primary',
            danger: true,
            customEvents: [
              {
                type: 'click',
                func: () => {
                  console.log(1);
                  alert('你点击了红色按钮');
                }
              }
            ]
          }
        ]
      });
      // instance.setFormData(form.getFieldsValue);

      const resFn = (res: any) => {
        console.log('SD PROCESS_SHOW', res);
        instance.postMessage({
          ...res,
          success: true,
          // formData: form.getFieldsValue()
        });
      };
      instance.registerAction('PROCESS_SHOW', resFn);
    }

    init();
    // btnFn();
  }, []);

  useEffect(() => {
    console.log(sdk);
    if (sdk) {
      const sendFn = (res:any) => {
        sdk.postMessage({
          ...res,
          s: 1,
          success: true,
          // formData: form.getFieldsValue()
        });
      };
      // sdk.registerAction('PROCESS_SHOW', resFn);
      // 拦截单个流程操作
      sdk.registerAction('SEND', sendFn);
      sdk.onProcessStatus((evt:any) => {
        console.log('onProcessStatus', evt);
      });

      // 拦截多个流程操作
      sdk.registerMoreAction(
        [
          PERMISSION_ENUM.GIVE,
          PERMISSION_ENUM.NODE_ADD,
          PERMISSION_ENUM.DRAFT_HANDLE
        ],
        (EVT: any) => {
          console.log('registerMoreAction', EVT);
        }
      );
    }
  }, [sdk]);


  return (
    <div>
      <form>
      </form>
    </div>
  );
};

export default ThirdForm;
